<template>
  <view class="container">
    <!-- 导航栏 -->
    <view class="nav-bar">
      <view class="left" @click="goBack">
        <image class="back-icon" src="/static/back-icon.png"></image>
      </view>
      <text class="nav-title">加油记录</text>
    </view>
    
    <!-- 记录列表 -->
    <scroll-view class="records-list" scroll-y>
      <!-- 首个记录项 -->
      <view class="record-item" :class="{'expanded': expandedIndex === 0}">
        <view class="record-header" @click="toggleRecord(0)">
          <text class="record-title">{{ fuelRecords[0].person }} | {{ fuelRecords[0].date }}</text>
          <image class="arrow-icon" :src="expandedIndex === 0 ? '/static/arrow-up2.png' : '/static/arrow-down2.png'"></image>
        </view>
        <view class="record-content" v-if="expandedIndex === 0">
          <view class="record-row">
            <text class="record-label">加油量</text>
            <text class="record-value">{{ fuelRecords[0].amount }} L</text>
          </view>
          <view class="record-row">
            <text class="record-label">加油金额</text>
            <text class="record-value">¥ {{ fuelRecords[0].cost }}</text>
            <text class="record-unit">元</text>
          </view>
          <view class="record-row">
            <text class="record-label">付款方式</text>
            <text class="record-value">{{ fuelRecords[0].paymentMethod }}</text>
          </view>
          <view class="record-row">
            <text class="record-label">附件</text>
            <text class="record-value">加油票据</text>
          </view>
          <view class="receipts-container" v-if="fuelRecords[0].receipts && fuelRecords[0].receipts.length > 0">
            <scroll-view class="receipts-scroll" scroll-x>
              <view class="receipts-list">
                <view class="receipt-item" v-for="(image, index) in fuelRecords[0].receipts" :key="index">
                  <image class="receipt-image" :src="image" mode="aspectFill" @click="previewImage(fuelRecords[0].receipts, index)"></image>
                </view>
              </view>
            </scroll-view>
          </view>
        </view>
      </view>

      <!-- 其他记录项 -->
      <view 
        class="record-item" 
        :class="{'expanded': expandedIndex === index+1}" 
        v-for="(record, index) in fuelRecords.slice(1)" 
        :key="index+1"
      >
        <view class="record-header" @click="toggleRecord(index+1)">
          <text class="record-title">{{ record.person }} | {{ record.date }}</text>
          <image class="arrow-icon" :src="expandedIndex === index+1 ? '/static/arrow-up2.png' : '/static/arrow-down2.png'"></image>
        </view>
        <view class="record-content" v-if="expandedIndex === index+1">
          <view class="record-row">
            <text class="record-label">加油量</text>
            <text class="record-value">{{ record.amount }} L</text>
          </view>
          <view class="record-row">
            <text class="record-label">加油金额</text>
            <text class="record-value">¥ {{ record.cost }}</text>
            <text class="record-unit">元</text>
          </view>
          <view class="record-row">
            <text class="record-label">付款方式</text>
            <text class="record-value">{{ record.paymentMethod }}</text>
          </view>
          <view class="record-row">
            <text class="record-label">附件</text>
            <text class="record-value">加油票据</text>
          </view>
          <view class="receipts-container" v-if="record.receipts && record.receipts.length > 0">
            <scroll-view class="receipts-scroll" scroll-x>
              <view class="receipts-list">
                <view class="receipt-item" v-for="(image, imgIndex) in record.receipts" :key="imgIndex">
                  <image class="receipt-image" :src="image" mode="aspectFill" @click="previewImage(record.receipts, imgIndex)"></image>
                </view>
              </view>
            </scroll-view>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      equipmentId: 'BH-001',
      expandedIndex: 0,
      fuelRecords: [
        {
          id: 1,
          person: '李四',
          date: '2023-01-04 12:15:25',
          amount: '125.36',
          cost: '12,365.26',
          location: '中石化浦东加油站',
          paymentMethod: '支付宝',
          receipts: ['/static/fapiao.png', '/static/fapiao.png']
        },
        {
          id: 2,
          person: '李旺',
          date: '2023-01-04 12:15:25',
          amount: '98.50',
          cost: '9,850.00',
          location: '中石油徐汇加油站',
          paymentMethod: '微信',
          receipts: ['/static/fapiao.png']
        },
        {
          id: 3,
          person: '李旺',
          date: '2023-01-04 12:15:25',
          amount: '110.25',
          cost: '11,025.75',
          location: '壳牌虹口加油站',
          paymentMethod: '现金',
          receipts: ['/static/fapiao.png','/static/fapiao.png','/static/fapiao.png','/static/fapiao.png']
        },
        {
          id: 4,
          person: '李旺',
          date: '2023-01-04 12:15:25',
          amount: '85.60',
          cost: '8,560.00',
          location: '中石化黄浦加油站',
          paymentMethod: '支付宝',
          receipts: ['/static/fapiao.png']
        },
        {
          id: 5,
          person: '李旺',
          date: '2023-01-04 12:15:25',
          amount: '105.80',
          cost: '10,580.00',
          location: '中石油静安加油站',
          paymentMethod: '微信',
          receipts: ['/static/fapiao.png']
        }
      ]
    }
  },
  onLoad(options) {
    if (options.id) {
      this.equipmentId = options.id;
    }
  },
  methods: {
    goBack() {
      uni.navigateBack({
        delta: 1
      });
    },
    toggleRecord(index) {
      this.expandedIndex = this.expandedIndex === index ? -1 : index;
    },
    previewImage(urls, current) {
      uni.previewImage({
        urls: urls,
        current: urls[current]
      });
    }
  }
}
</script>

<style>
.container {
  flex: 1;
  background-color: #F5F7FA;
  width: 100%;
}

.nav-bar {
  height: 88px;
  background: linear-gradient(103deg, #4980FF -4%, #2B6BFF 99%);
  padding-top: 44px;
  padding-left: 15px;
  padding-right: 15px;
  flex-direction: row;
  align-items: center;
  position: relative;
}

.left {
  position: absolute;
  left: 15px;
  height: 24px;
  width: 24px;
  justify-content: center;
  align-items: center;
}

.back-icon {
  width: 24px;
  height: 24px;
}

.nav-title {
  font-size: 18px;
  color: #FFFFFF;
  font-weight: 500;
  margin-left: 24px;
}

/* 记录列表 */
.records-list {
  flex: 1;
  width: 100%;
  height: calc(100vh - 88px); /* 减去导航栏高度 */
}

.record-item {
  background-color: #FFFFFF;
  margin-bottom: 1px;
}

.record-header {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #EEEEEE;
}

.record-title {
  font-size: 16px;
  color: #333333;
}

.arrow-icon {
  width: 16px;
  height: 16px;
}

.record-content {
  padding: 0 15px 0 30px;
}

.record-row {
  flex-direction: row;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #EEEEEE;
}

.record-label {
  width: 80px;
  font-size: 16px;
  color: #333333;
}

.record-value {
  flex: 1;
  font-size: 16px;
  color: #333333;
}

.record-unit {
  font-size: 16px;
  color: #333333;
  margin-left: 10px;
}

.receipts-container {
  padding: 15px 0;
}

.receipts-scroll {
  width: 100%;
}

.receipts-list {
  flex-direction: row;
}

.receipt-item {
  width: 80px;
  height: 80px;
  margin-right: 10px;
  border-radius: 4px;
  overflow: hidden;
}

.receipt-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style> 